local iso8583 = require("iso8583")

--local iso8583_str = "0100202000000080801B350000000453313331303532303231353600183030303030303030303030303030303030300002009A002031313130303031393030303039363330303033353242443430443338"
--local iso8583_str = "30313030F23C2481A8E0980000000000000001003136343237363535353535353535353535353030303030303030303030303037373730303032313031313431323031313431323031333138343430373031313930323634333930313032303631323334353630363132333435363337343237363535353535353535353535353D313233343536373839303132333435363738393039383736353433323130303130303030303332313132303030303030303030303033342020202020202020202020202020202020202020202020202020202020202054657374207465787436"
--local iso8583_str="30313030f23a44c18ce090100000004010000020313636323236333039383130303031313233323030303030303030303030303030303031303432393133323432323033303436353131313435393033333130333331202020203031323036303030333235303034303230303130323432313039313336353231393630373030353139353538333034313130303730313130313233202020202020202020202020202020202020202020202020202020202020202020202020202020203135363030303030303030303134303030303030303030363030303030313030303931333635313231393130323432313235302020202020202020303230302020202020202031313030303030303030303030303432202020202020202020202020202020202020202020202020202020202020303033303436353030303030"

--local iso8583_str="30313030f23c2481a8e0980000000000000001003136343237363535353535353535353535353030303030303030303030303037373730303033313630393238303230393238303231333138343430373031313930323634333930313032303630303031313130363030303131313337343237363535353535353535353535353d3132333435363738393031323334353637383930393837363534333231303031303030303033323131323030303030303030303030333420202020202020202020202020202020202020202020202020202020202020546573742074657874363433010203040506070831323334303030303030303030303030303137416e6f7468657220746573742074657874"

local iso8583_str="30313030f23c2481a8e098000000000000000100313634323736353535353535353535353535303030303030303030303030303737373030303331363132303531363132303531363133313834343037303119023634333930313032303630303031313130363030303131313337343237363535353535353535353535353d3132333435363738393031323334353637383930393837363534333231303031303030303033323131323030303030303030303030333420202020202020202020202020202020202020202020202020202020202020546573742074657874363433010203040506070831323334303030303030303030303030303137416e6f7468657220746573742074657874"

local function to_bin(str)
    return ({str:gsub("..", function(x) return string.char(tonumber(x, 16)) end)})[1]
end

local function to_hex(str)
    return ({str:gsub(".", function(c) return string.format("%02X", c:byte(1)) end)})[1]
end

print(iso8583_str)


iso8583_str = to_bin(iso8583_str)

local parser, err = iso8583.new({
        [14] = { size = 4,   type = iso8583.FIX,     align = iso8583.L, pad = '0', compress = iso8583.Z },
        [43] = { size = 40,  type = iso8583.FIX,     align = iso8583.L, pad = ' ', compress = iso8583.U },
        [44] = { size = 99,  type = iso8583.LLVAR,   align = iso8583.R, pad = '0', compress = iso8583.U },
        [48] = { size = 999, type = iso8583.LLLVAR,  align = iso8583.L, pad = 'F', compress = iso8583.U },
        [52] = { size = 8,   type = iso8583.FIX,     align = iso8583.R, pad = '0', compress = iso8583.U },
        [53] = { size = 16,  type = iso8583.FIX,     align = iso8583.R, pad = '0', compress = iso8583.U },
       [120] = { size = 999, type = iso8583.LLLVAR_U,align = iso8583.L, pad = 'F', compress = iso8583.U },
})

print (err)

local msg, err = parser:Unpack(iso8583_str)

print (err)

for k, v in pairs(msg) do
    if k == 52 then 
	print(k,':',to_hex(v))
    else
	print(k,':',v)
    end
end

local packed_str, err = parser:Pack(msg)

if not packed_str then
	print(err)
	return
end

local packed_str_hex = to_hex(packed_str)

print(packed_str_hex)
